<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="ttp://www.security.com/tag" prefix="sc"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="1"> 
<script src="js/alai_tree/alai_tree.js"></script>
<script src="js/alai_tree/alai_tree_pretty.js"></script>
<script src="js/alai_tree/alai_tree_check.js"></script>
<script src="js/prototype-1.4.0.js"></script>
<script src="js/InputTool.js"></script>
<script src='js/comm.js'></script>
<link href="template/css.css" rel="stylesheet" type="text/css">
<title>departmentTree</title>
</head>

<body>
<table width="100%" height="100%" border="0">
	<tr>
		<td width="20%" background="images/treebg.gif" valign="top" bordercolor="#7d7be3">
			<table><tr>
				<td>选择操作：</td>
				<td colspan="10">
					<select name="selectFun" onchange="fireFun()" >
						<option value="detail" >查看部门</option>
						<sc:component processcode="department_add"><option value="add">添加部门</option></sc:component>
						<option selected value="member">查看成员</option>
						<sc:component processcode="department_edit"><option value="edit">编辑部门</option></sc:component>
						<option value="fresh">刷新部门树</option>
					</select>
				</td>
			</tr></table>
			<div  id="divTree1" style="overflow: auto;width: 200;height: 300;float: none;"></div>
		</td>
		<td width="80%" bordercolor="#b4fef4">
			<iframe width="100%" src="blank.jsp" frameborder="0" height="100%" name="departmentworkPlace" src=""></iframe>
		</td>
	</tr>
</table>
</body>
</html>
<script>

var tree1=new alai_tree_pretty(divTree1)
var root=tree1.root

function setFun(){
	
	var functionf=getSelectValue(document.all.selectFun);
	if('add'==functionf){
		//if(!validateSelTree())return false;
		departmentAdd();
	}else if('detail'==functionf){
		if(!validateSelTree())return false;
		departmentDetail();
	}else if('member'==functionf){
		if(!validateSelTree())return false;
		showMember();
	}else if('edit'==functionf){
		if(!validateSelTree())return false;
		departmentEdit();
	}
}
function validateSelTree(){
	var treeNode=tree1.getSelectedNode();
	if(!treeNode){
		alert("请选择一个部门");
		return false;
	}
	return true;
}

function fireFun(){
	setFun();
	var functionf=getSelectValue(document.all.selectFun);
	if("fresh"==functionf){
		freshTree();
	}
}

function loadTreeNode(pidNode){
		var pid="-2";
		if(pidNode.body)pid=pidNode.body.pid
		var url="<%=request.getContextPath()%>/departmenttreenode.action?pid="+pid;
		var myAjax = new Ajax.Request(
					url, 
					{ 
						method: 'get', 
						asynchronous:false
				}); 
		var myXML = myAjax.transport.responseXML;
		addNodeToTree(myXML,pidNode);
	}
	
	function addNodeToTree(doc,pidNode){
		var nodes=doc.selectNodes("TREENODES/TREENODE");
		if(nodes.length>0){
			var node,treeNode;
			for(var i=0;i<nodes.length;i++){
				node=nodes[i];
				treeNode=pidNode.add(node.getAttribute('name'));
				treeNode.expand(false);
				treeNode.body.flg=0;
				treeNode.body.pid=node.getAttribute('id')
				treeNode.icon.src='js/alai_tree/images/folderclose.gif'
			}
		}
	}
	
function treeClkListener(){
	tree1.onclick=function(treeNode){
		var flg=treeNode.body.flg;
		if(0==flg){
			loadTreeNode(treeNode);
			treeNode.body.flg=1;
			treeNode.expand();
		}
		treeNode.expand();
		if(treeNode.expanded)treeNode.icon.src='js/alai_tree/images/folderopen.gif'
		setFun();
	}
}
treeClkListener()
function showMember(){
	var treeNode=tree1.getSelectedNode();
	var departmentid=treeNode.body.pid
	document.all.departmentworkPlace.src="userlistList.action?condition=departmentId="+departmentid;
}
function departmentAdd(){
	var treeNode=tree1.getSelectedNode();
	var departmentid="-1"
	if(treeNode){
		departmentid=treeNode.body.pid
	}
	document.all.departmentworkPlace.src="departmentAdd.action?fromTree=1&pid="+departmentid;
}
function departmentEdit(){
	var treeNode=tree1.getSelectedNode();
	var departmentid=treeNode.body.pid
	document.all.departmentworkPlace.src="departmentEdit.action?fromTree=1&id="+departmentid;
}
function departmentDetail(){
	var treeNode=tree1.getSelectedNode();
	var departmentid=treeNode.body.pid
	document.all.departmentworkPlace.src="departmentDetail.action?fromTree=1&id="+departmentid;
}
loadTreeNode(root);
function freshTree(){
	divTree1.innerHTML='';
	tree1=new alai_tree_pretty(divTree1)
	root=tree1.root
	loadTreeNode(root);
	treeClkListener();
}
</script>

		
